package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.result.ResultUtil;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.common.util.StringUtils;
import com.ctcemti.mdm.project.sys.entity.OrgOrganization;
import com.ctcemti.mdm.project.sys.service.ICommonService;
import com.ctcemti.mdm.project.sys.service.ICompanyService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("company")
@Api(value = "CompanyController", description = "公司控制器类")
public class CompanyController {

    @Autowired
    ICompanyService companyService;

    @Autowired
    ICommonService commonService;

    @PostMapping("insertOrUpdate")
    public Result insertCompany(HttpServletRequest request,OrgOrganization organization){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        String loginName = JWTUtil.getLoginName(request.getHeader("Authorization"));
        organization.setCreator(loginId);
        organization.setModifiedBy(loginId);
        organization.setModifyDate(new Date());
        Result result = companyService.insertOrUpdate(organization);
        result.setRefreshToken(request);
        return result;
    }

    @PostMapping("delete")
    public Result delete(HttpServletRequest request,OrgOrganization orgOrganization){
        if(StringUtils.isNullOrEmpty(orgOrganization.getId())){
            return ResultUtil.error(ResultEnum.ZTSJ_1008);
        }
        Result result = companyService.delete(orgOrganization );
        result.setRefreshToken(request);
        return result;
    }

    @PostMapping("deleteAll")
    public Result deleteAll(HttpServletRequest request,String idStr){
        if(StringUtils.isNullOrEmpty(idStr)){
            return ResultUtil.error(ResultEnum.ZTSJ_1001);
        }
        //循环删除
        String[] ids = idStr.split(",");
        if(ids.length>0){
            for(String id : ids){
                List<OrgOrganization> orgOrganizations = companyService.selectChildById(id);
                if(orgOrganizations != null){
                    for(OrgOrganization orgOrganization:orgOrganizations){
                        companyService.delete(orgOrganization);
                    }
                }
            }
        }
        return new Result(ResultEnum.SUCCESS,null);
    }

    @PostMapping("getByPid")
    public Result getByPid(HttpServletRequest request,String id){
        if(StringUtils.isNullOrEmpty(id)){
            return ResultUtil.error(ResultEnum.ZTSJ_1000);
        }
        Result result = companyService.getByPid(id);
        result.setRefreshToken(request);
        return result;
    }

    @PostMapping("getByPersonId")
    public Result getByPersonId(HttpServletRequest request,String personId){
        Result result = new Result();
        if(personId!=null){
            commonService.insertComRelationToRedisByPersonId(personId);
            result = companyService.getByPersonId(personId);
        }
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        String loginName = JWTUtil.getLoginName(request.getHeader("Authorization"));
        if(StringUtils.isNullOrEmpty(loginId)){
            return ResultUtil.error(ResultEnum.ZTSJ_1008);
        }
        //插入关系
        if("Administrator".equals(loginName)){
            //commonService.insertComRelationToRedisByPersonIdForAdministrator(loginId);
            result = new Result(ResultEnum.SUCCESS,companyService.selectRootById());
        }else{
            //commonService.insertComRelationToRedisByPersonId(loginId);
            result = companyService.getByPersonId(loginId);
        }
        result.setRefreshToken(request);
        return result;
    }



    @PostMapping("getOrgForTree")
    public Result getOrgForTreeByName(HttpServletRequest request,String shortName){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        List<OrgOrganization> result = companyService.getOrgForTreeByName(request, shortName, loginId);
        return new Result<OrgOrganization>(ResultEnum.SUCCESS,result);
    }






}
